﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class StaffController : Controller
    {
        private UserBLO _userBLO = new UserBLO();

        /// <summary>
        /// display all staffs
        /// </summary>
        /// <returns>list of staffs</returns>
        /// <author>hungph</author>
        public ActionResult Index()
        {
            return View(_userBLO.DisplayAllStaffs());
        }

        /// <summary>
        /// render View Create staff
        /// </summary>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Create()
        {
            ViewBag.UserName = null;
            ViewBag.Email = null;

            List<String> listStrGender = new List<String>();
            listStrGender.Add("Nam");
            listStrGender.Add("Nữ");
            ViewBag.Gender = new SelectList(listStrGender);

            return View();
        }

        /// <summary>
        /// create staff
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        [HttpPost]
        public ActionResult Create(User user)
        {
            if (_userBLO.CheckUsername(user))
            {
                ViewBag.UserName = user.UserName;
            }
            if (_userBLO.CheckEmail(user))
            {
                ViewBag.Email = user.Email;
            }
            if (ModelState.IsValid)
            {
                if (!_userBLO.CheckUsername(user) && !_userBLO.CheckEmail(user))
                {
                    ViewBag.UserName = null;
                    ViewBag.Email = null;
                    _userBLO.CreateStaff(user);
                    return RedirectToAction("Index");
                }
            }

            List<String> listStrGender = new List<String>();
            listStrGender.Add("Nam");
            listStrGender.Add("Nữ");
            ViewBag.Gender = new SelectList(listStrGender);

            return View(user);
        }

        /// <summary>
        /// active staff
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Active(String id)
        {
            User user = _userBLO.LoadUserByUserName(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            bool isActive = _userBLO.ActiveUser(user);
            return RedirectToAction("Index");
        }

        /// <summary>
        /// deactive staff
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Deactive(String id)
        {
            User user = _userBLO.LoadUserByUserName(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            bool isActive = _userBLO.DeactiveUser(user);
            return RedirectToAction("Index");
        }

        /// <summary>
        /// load staff and render view edit info of staff
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        public ActionResult Edit(String id)
        {
            User user = _userBLO.LoadUserByUserName(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            List<String> listStrGender = new List<String>();
            if (user.Gender == "Nam")
            {
                listStrGender.Add("Nam");
                listStrGender.Add("Nữ");
            }
            else
            {
                listStrGender.Add("Nữ");
                listStrGender.Add("Nam");
            }
            ViewBag.Gender = new SelectList(listStrGender);
            this.Session["User"] = user.UserName;
            return View(user);
        }

        /// <summary>
        /// edit info of staff
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        [HttpPost]
        public ActionResult Edit(User user)
        {
            var email = _userBLO.LoadUserByUserName(this.Session["User"].ToString().Trim()).Email;
            if (email == user.Email)
            {
                ViewBag.Email = null;
            }
            if (email != user.Email)
            {
                ViewBag.Email = user.Email;
            }
            if (ModelState.IsValid)
            {
                user.IsActive = _userBLO.LoadUserByUserName(this.Session["User"].ToString().Trim()).IsActive;
                user.IsDeleted = _userBLO.LoadUserByUserName(this.Session["User"].ToString().Trim()).IsDeleted;
                var isExist = _userBLO.CheckEmail(user);
                if (!isExist || (ViewBag.Email == null))
                {
                    var isSuccess = _userBLO.EditStaff(user);
                    if (isSuccess)
                    {
                        this.Session["User"] = null;
                        return RedirectToAction("Index");
                    }
                }
            }
            List<String> listStrGender = new List<String>();
            if (user.Gender == "Nam")
            {
                listStrGender.Add("Nam");
                listStrGender.Add("Nữ");
            }
            else
            {
                listStrGender.Add("Nữ");
                listStrGender.Add("Nam");
            }
            ViewBag.Gender = new SelectList(listStrGender);
            return View(user);
        }

        /// <summary>
        /// Delete user
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Redirect to Index page</returns>
        /// <author>hungph</author>
        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(String id)
        {
            User user = _userBLO.LoadUserByUserName(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            _userBLO.DeleteUser(user);
            return RedirectToAction("Index");
        }
    }
}
